package de.tuberlin.julia.SFD_Numbers;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import com.google.code.microlog4android.Logger;
import com.google.code.microlog4android.LoggerFactory;

import de.tlabs.ahmad.trigger_server.TriggerServer;

import experiment.ApplyDesign;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class ButtonPressedListener implements OnClickListener{
	
	private static final Logger logger = LoggerFactory.getLogger();

	private ApplyDesign applyDesign = null;
	
	NumberView numberView;
	Thread feedbackThread;

    TimeZone tz = TimeZone.getTimeZone("Europe/Berlin");
    Locale locale = Locale.GERMANY;
    final Calendar c = Calendar.getInstance(tz,locale);   	            
    SimpleDateFormat f= new SimpleDateFormat("HH:mm:ss.SS");
    
	public ButtonPressedListener(NumberView numberView, ApplyDesign applyDesign) {
		this.numberView = numberView;
		this.applyDesign = applyDesign;
	}

	@Override
	public void onClick(View v) {
		if (v instanceof TextView) {			
			TextView bt = (TextView) v;
			Object tag = bt.getTag();
			String digit = bt.getText().toString();
			if (!"C".equals(tag) && !"enter".equals(tag)) {
				logger.info("clicked");
				TriggerServer.getOutputStream().print(25);
				TriggerServer.getOutputStream().flush();			
	            
	            f.setTimeZone(c.getTimeZone());
	            Date date = c.getTime();
	            logger.info("no feedback " + f.format(date));
	            
				applyDesign.enterDigit(digit);
				applyDesign.compareDigits(feedbackThread); 
			}

		}
	}

}
